// import store from '/store/index.js' //vuex  
let server_url = 'http://8.130.32.118:8080' //请求地址
// let token = ' '; 凭证
process.env.NODE_ENV === 'development' ? server_url : 'http://***/api' //环境配置
function service(options = {}) {
	// store.state.token && (token = store.state.token) //从vuex中获取登录凭证
	options.url = `${server_url}${options.url}`
	console.log(options.url)
	//配置请求头
	options.header = {
		'content-type': 'application/json',
		// 'accessToken': `${token}` //Bearer 
	}

	return new Promise((resolved, rejected) => {
		//成功
		options.success = res => {
				console.log(res)
			if (Number(res.data.code) === 200||Number(res.data.code) === 201) { //请求成功
				console.log(res.data)
				resolved(res.data)
			} else {
				uni.hideLoading()
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: `${res.data.msg}`
				})
				rejected(res.data.msg) //错误
			}

		}
		//错误
		options.fail = err => {
			uni.hideLoading()
			uni.showToast({
				icon: 'none',
				duration: 3000,
				title: '请求错误'
			})
			rejected(err) //错误
		}
		uni.request(options)

	})
}
export default service
